<?php
/**
 * @version $Id: plg_content_contentads.php 1.0.4 for joomla 2.5+
 * @copyright Juan Padial
 * @license GNU/GPLv3,
 * @author Juan - http://www.shikle.com
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.utilities.arrayhelper');
class plgContentContentads extends JPlugin {
	function plgContentScontent(&$subject,$config)
	{
		parent::__construct($subject,$config);
	}
    function onContentBeforeDisplay($context,&$row,&$params,$page=0) {
       $app = &JFactory::getApplication();
        if (!$app->isSite() || $this->noads($row)) {
            return;
        } else {
           $ads = $this->showContent('before_content_ads', true);
           $css = $this->params->def('cssb','');
           if($ads) return $this->theAd($ads,$css); else return '';
        }
    }
    function onContentPrepare($context, $row, &$params, $page = 0) {
        if ((JRequest :: getVar('view')) != 'article' || $context!='com_content.article' || $this->noads($row)) {
            return;
        }
        $topAds = $this->params->def('content_top_ads','');
        if ($topAds) {
              $css = $this->params->def('csst','');
              $row->text = $this->theAd($topAds,$css).$row->text;
        }
        $insideAds = $this->params->def('content_inside_ads','');
        if ($insideAds) {
            $css = $this->params->def('cssin','');
            (int) $position = $this->params->def('in_position',3);
            $arraytext = explode('</p>',$row->text);
            count($arraytext)<$position?$position=count($arraytext):'';
            if(!empty($arraytext)){
               array_splice($arraytext,$position,0,$this->theAd($insideAds,$css));
               $output = '';
               foreach($arraytext as $key=>$value){
                 $output .= $value;
               }
               $row->text = $output;
            }
        }
        $bottomAds = $this->params->def('content_bottom_ads');
        if ($bottomAds) {
            $css = $this->params->def('cssbo','');
            $row->text .= $this->theAd($bottomAds,$css);
        }
    }

    function onContentAfterDisplay($context, &$row, &$params, $page = 0) {
       $app = &JFactory::getApplication();
        if (!$app->isSite() || $this->noads($row)) {
            return;
        } else {
           $ads = $this->showContent('after_content_ads', true);
           $css = $this->params->def('cssa','');
           $css != '' ? $css = ' style="'.$css.'"':'';
           if($ads) return '<div'.$css.'>'.$ads.'</div>';
        }
        return '';
    }
    function showContent($paramName, $isCount) {
        JRequest::getVar('layout')=='blog'?$isblog=true:$isblog=false;
        if (!$isblog) {
            return $this->params->def($paramName);
        } else {
            $blogPageCount = JRequest :: getVar('blogPageAdsCount');
            if (!isset($blogPageCount)) {
                $blogPageCount = 0;
            }
            if ($isCount) {
                $blogPageCount++;
                JRequest :: setVar('blogPageAdsCount', $blogPageCount);
            }
            $adCount = (int) $this->params->def('blog_page_ads_count');

            if ($blogPageCount <= $adCount) {
                if ($isblog && $blogPageCount <= 1) {
                    return $this->params->def($paramName);
                } else {
                    return $this->params->def($paramName);
                }
            }
        }
    }

   function theAd($ads,$css){
        $css != '' ? $css = ' style="'.$css.'"':'';
        return '<div'.$css.'>'.$ads.'</div>';
   }
   function exclude($paramName,$value,$tointeger=true) {
        $excludearray = explode(',', trim($this->params->def($paramName)));
        $tointeger?JArrayHelper::toInteger($excludearray):'';
        if(isset($value) && in_array($value, $excludearray, false)) {
            return true;
        } else {
            return false;
        }
    }
    function noads(&$row) {
        if(!is_object($row)) return;
        $onlyGuest = $this->params->def('only_guest');
        if ($onlyGuest) {
            $user = & JFactory::getUser();
            if(!$user->guest) {
                return true;
            }
        }
        $createdBy = $row->created_by;
        $authors = $this->params->def('only_authors');
        if (isset($authors) && $authors!='') {
            $authorsArray = explode(',', $authors);
            if (!empty($authorsArray) && $authors!='' && !in_array($createdBy, $authorsArray, false)) {
                return true;
            }
        }
        $app = & JFactory::getApplication();
        $template = $app->getTemplate();
        $template = strtolower($template);
        $templates = $this->params->def('only_templates');
        $templatesArray = explode(',', $templates);
        if (!empty($templatesArray) && $templates!='' && !in_array($template, $templatesArray)) {
            return true;
        }
        $ip = $this->GetUserIp();
        $ignore = $this->exclude('block_ips',$ip,false);
        if ($ignore) {
            return $ignore;
        }
        $ignore = $this->exclude('exclude_article_ids', $row->id);
        if ($ignore) {
            return $ignore;
        }
        $ignore = $this->exclude('exclude_category_ids', $row->catid);
        if ($ignore) {
            return $ignore;
        }
    }
//function from CBreCaptcha
function GetUserIp() {
   $ip = "";
   if(isset($_SERVER)) {
       if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
         $ip=$_SERVER['HTTP_CLIENT_IP'];
       } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
         $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
       } else {
         $ip=$_SERVER['REMOTE_ADDR'];
       }
    } else {
       if ( getenv( 'HTTP_CLIENT_IP' ) ) {
          $ip = getenv( 'HTTP_CLIENT_IP' );
       } elseif ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
          $ip = getenv( 'HTTP_X_FORWARDED_FOR' );
       } else {
          $ip = getenv( 'REMOTE_ADDR' );
        }
    }
    // In some weird cases the ip address returned is repeated twice separated by comma
    if(strstr($ip,',')){
      $ip = array_shift(explode(',',$ip));
    }
   return $ip;
 }
}
?>